package com.skytech.tax.customsdatawarehouse.singlewindow.dictcategory.domain.dao;

import com.skytech.tax.customsdatawarehouse.singlewindow.dictcategory.domain.entity.DictCategory;
import com.skytech.tax.customsdatawarehouse.singlewindow.dictcategory.domain.entity.DictCategoryItem;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DictCategoryDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String insertDictCategorySql = "INSERT INTO dict_category (categoryKey, categoryName, sortIdx)" +
            " VALUES (:categoryKey, :categoryName, :sortIdx)";

    public void save(List<DictCategory> list) {
        SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(list);
        namedParameterJdbcTemplate.batchUpdate(insertDictCategorySql, batch);
    }

    private String deleteDictCategorySql = "DELETE FROM dict_category WHERE categoryKey IN (:categoryKeys)";

    public void deleteBy(List<String> categoryKeys) {
        Map<String, Object> params = new HashMap<>();
        params.put("categoryKeys", categoryKeys);
        namedParameterJdbcTemplate.update(this.deleteDictCategorySql, params);
    }

    private String insertDictCategoryItemSql = "INSERT INTO dict_categoryitem (categoryKey, itemKey, itemName)" +
            " VALUES (:categoryKey, :itemKey, :itemName)";

    public void saveDictCategoryItem(List<DictCategoryItem> list) {
        SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(list);
        namedParameterJdbcTemplate.batchUpdate(insertDictCategoryItemSql, batch);
    }

    private String deleteTaxBillSql = "DELETE FROM dict_categoryitem WHERE (categoryKey,itemKey) IN ((:categoryKey,:itemKey))";

    public void deleteByDictCategoryItem(List<DictCategoryItem> items) {
        SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(items);
        namedParameterJdbcTemplate.batchUpdate(this.deleteTaxBillSql, batch);
    }

    private String existsDictCategorySql = "SELECT count(1) FROM dict_categoryitem WHERE categoryKey = ?";

    public boolean existsDictCategory(String categoryCode) {
        Integer count = namedParameterJdbcTemplate.getJdbcOperations()
                .queryForObject(this.existsDictCategorySql, new Object[]{categoryCode}, Integer.class);
        return count != null && count > 0;
    }


    private String getDictCategoryItemSql = "select * from dict_categoryitem where 1=1";

    public List<DictCategoryItem> getDictCategoryItems(String categoryCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("categoryCode", categoryCode);
        return namedParameterJdbcTemplate.query(this.getDictCategoryItemSql + " AND categoryKey in (:categoryCode)", params, new BeanPropertyRowMapper<>(DictCategoryItem.class));
    }

}
